/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import VO.CProfesor;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author HELIOS
 */
public class CProfesoresDAO implements IFundacionDAO {

    @Override
    public boolean insert(Object obj) {
        boolean flag = false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("FundacionProfesoresPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.persist((CProfesor) obj);
            trans.commit();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al registrar un profesor en el sistema " + ex.getMessage());
            trans.rollback();
        } finally {
            em.close();
        }
        return flag;
    }

    @Override
    public boolean update(Object obj) {
        boolean flag = false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("FundacionProfesoresPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.merge((CProfesor) obj);
            trans.commit();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error actuaizando datos de profesor " + ex.getMessage());
            trans.rollback();
        } finally {
            em.close();
        }
        return flag;
    }

    @Override
    public boolean delete(Object obj) {
        boolean flag = false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("FundacionProfesoresPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction trans = em.getTransaction();
        try {
            trans.begin();
            em.remove(em.merge((CProfesor) obj));
            trans.commit();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error eliminando profesor " + ex.getMessage());
            trans.rollback();
        } finally {
            em.close();
        }
        return flag;
    }
    
    public CProfesor buscarProfesorPorCedula(String numDoc){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("FundacionProfesoresPU");
        EntityManager em = emf.createEntityManager();
        return em.find(CProfesor.class, numDoc);
    }
}
